unit clsMotCanc;

interface

uses Dm, dbtables, biblio;

type
  TMotCancPedido = class(TObject)
  private
    Canp_codigo: string;
  public
    Canp_descricao: string;
    function Gravar(p_Canp_codigo: string): Byte;
    function Recuperar(p_Canp_codigo: string): Byte;
    function Excluir(p_Canp_codigo: string): Byte;
    procedure Consultar(var p_q: TQuery; p_nome: string);
  end;

implementation

function TMotCancPedido.Gravar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM MotCancPedido where Canp_codigo=:k_Canp_codigo');
  tq.parambyname('k_Canp_codigo').asstring := strzero(p_Canp_codigo, 3);
  tq.open;
  if tq.eof then begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('INSERT INTO MotCancPedido VALUES(:k_Canp_codigo,:k_Canp_descricao)');
  end else begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('UPDATE MotCancPedido SET Canp_descricao=:k_Canp_descricao WHERE Canp_codigo=:k_Canp_codigo');
  end;
  tq.parambyname('k_Canp_codigo').asstring := strzero(p_Canp_codigo, 3);
  tq.parambyname('k_Canp_descricao').asstring := Canp_descricao;
  tq.close;
  tq.execsql;
  result := 1;
  tq.close;
  tq.Free;
end;

function TMotCancPedido.Recuperar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM MotCancPedido where Canp_codigo=:k_Canp_codigo');
  tq.Params[0].asstring := strzero(p_Canp_codigo, 3);
  tq.open;
  Canp_codigo := p_Canp_codigo;
  if tq.eof then begin
    Canp_descricao := '';
    Result := 0;
  end else begin
    Canp_descricao := tq.fieldbyname('Canp_descricao').asstring;
    Result := 1;
  end;
  tq.Close;
  tq.Free;
end;

function TMotCancPedido.Excluir;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM MotCancPedido WHERE Canp_codigo=:k_Canp_codigo');
  tq.parambyname('k_Canp_codigo').asstring := strzero(p_Canp_codigo, 3);
  tq.execSQL;
  Result := 1;
  tq.close;
  tq.Free;
end;

procedure TMotCancPedido.Consultar;
begin
  p_q.sql.Clear;
  p_q.Params.Clear;
  p_q.sql.add('Select * from MotCancPedido where Canp_descricao>=:k_Canp_descricao order by Canp_descricao');
  p_q.parambyname('k_Canp_descricao').asstring := p_nome;
end;

end.
